package com.lanou.bookstore.book.service;

import com.lanou.bookstore.book.dao.BookDao;
import com.lanou.bookstore.book.dao.BookDaoImpl;
import com.lanou.bookstore.book.domain.Book;
import com.lanou.bookstore.category.dao.CategoryDao;
import com.lanou.bookstore.category.dao.CategoryDaoImpl;
import com.lanou.bookstore.category.domain.Category;

import java.util.List;

public class BookServiceImpl implements BookService {

    BookDao bookDao = new BookDaoImpl();
    CategoryDao cateDao = new CategoryDaoImpl();

    @Override
    public List<Book> queryAll() {
        List<Book> list = bookDao.queryAll();
        return list;
    }

    @Override
    public List<Book> queryByName(String bookName) {
        List<Book> list = bookDao.queryByCate(bookName);
        return list;
    }

    @Override
    public List<Book> queryByBid(String bId) {

        List<Book> list = bookDao.queryByDid(bId);
        return list;
    }

    @Override
    public void addBook(Book book) throws Exception {
        int max = -1;
        List<Book> list = bookDao.queryAll();
        for (Book book1 : list) {
            if (book.getBname().equals(book1.getBname())){

                throw new Exception("此图书已经存在!!!");

            }
            if (Integer.parseInt(book1.getBid()) > max){
                max = Integer.parseInt(book1.getBid());
            }
        }
        max++;

        List<Category> queryAllList = cateDao.queryAll();
        for (Category category : queryAllList) {
            if (category.getCname().equals(book.getCid())){
                book.setCid(category.getCid());
            }
        }

//        bid,bname,price,author,image,cid


        bookDao.addBook(book, max);


    }

    @Override
    public List<Book> decide(String para) {

        if (para.equals("all")){
            return queryAll();
        }else {
            return queryByName(para);
        }

    }
}
